<template>
  <div>
    <div class="allsearch">
      <section class="search-content">
        <el-row :gutter="4" type="flex" justify="start">
          <el-col :span="21">
            <el-cascader
              :change-on-select="true"
              :options="typeGroups"
              v-model="classList.type"
              @change="handleFilter"
            />
            <el-select v-model="classList.status" placeholder="班级状态" style="width:120px;" @change="handleFilter">
              <el-option v-for="item in statusList" :key="item.value" :value="item.value" :label="item.label"/>
            </el-select>
            <el-input v-model="classList.title" placeholder="标题" style="width:200px;" @keyup.enter.native="handleFilter"/>
            <el-button type="primary" size="medium" @click="handleFilter">搜索</el-button>
          </el-col>
        </el-row>
      </section>
    </div>
    <!-- 表格 -->
    <subTable
      :table-label="tableLabel"
      :table-data="listFilter.slice((currentpage-1)*pagesize,currentpage*pagesize)"
    >
      <el-table-column slot="button" :min-width="2" label="操作" prop="manage" align="center">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleCommand(scope)">选择</el-button>
        </template>
      </el-table-column>
    </subTable>
    <!-- 分页组件 -->
    <pagination
      :total="total"
      :current-page="currentpage"
      :page-size="pagesize"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
  </div>
</template>

<script>
import SubTable from '@/components/Eltable'
import Pagination from '@/components/Pagination' // 分页组件
import { getClassList } from '@/api/operate/promcode' // 数据接口
import { getCategoryList } from '@/api/subject/category'
import { getTenantId } from '@/utils/auth'
export default {
  components: {
    Pagination,
    SubTable
  },
  data() {
    return {
      total: 0,
      currentpage: 1,
      pagesize: 20,
      classList: {
        type: [],
        status: '',
        title: ''
      },
      tableLabel: [
        {
          label: '编号', width: '1', prop: 'id'
        }, {
          label: '名称', width: '4', prop: 'title', isOpera: true
        }, {
          label: '学员数', width: '2', prop: 'studentNum'
        }, {
          label: '价格', width: '2', prop: 'price'
        }, {
          label: '班级状态', width: '2', prop: 'statusStr'
        }
      ],
      tableData: [],
      listFilter: [],
      typeGroups: [
        {
          label: '班级分类',
          value: ''
        }
      ],
      statusList: [
        {
          label: '班级状态',
          value: ''
        }, {
          label: '未发布',
          value: '未发布'
        }, {
          label: '已发布',
          value: '已发布'
        }, {
          label: '已关闭',
          value: '已关闭'
        }
      ]
    }
  },
  mounted() {
    this.getClassList();
    this.getCategoryList()
  },
  methods: {
    handleCommand(scope) {
      this.$emit('choiceData', scope.row)
    },
    getCategoryList() {
      getCategoryList('class', getTenantId(),'0').then(response => {
        response.data.items.forEach(item => {
          item.label = item.name;
          item.value = item.name;
          if (item.parentId === 0) {
            item.children = [];
            this.typeGroups.push(item)
          }
        });
        response.data.items.forEach(item => {
          if (item.parentId !== 0) {
            item.label = item.name;
            item.value = item.name;
            this.typeGroups.forEach(category => {
              if (item.parentId === category.id) {
                category.children.push(item)
              }
            })
          }
        })
      })
    },
    handleSizeChange(val) {
      this.currentpage = 1;
      this.pagesize = val.value;
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    handleCurrentChange(val) {
      this.currentpage = val.value;
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    handleFilter() {
      this.listFilter = this.tableData.filter(item => {
        if (this.classList.type[0] && item.categoryName !== this.classList.type[this.classList.type.length - 1]) return false;
        if (this.classList.status && item.statusStr !== this.classList.status) return false;
        if (this.classList.title && item.title.toLocaleLowerCase().indexOf(this.classList.title.toLocaleLowerCase()) === -1) return false;
        return true
      });
      this.total = this.listFilter.length
    },
    getClassList() {
      getClassList().then(response => {
        response.data.items.map(item => {
          if (item.status === 'closed') item.statusStr = '已关闭';
          if (item.status === 'draft') item.statusStr = '未发布';
          if (item.status === 'published') item.statusStr = '已发布'
        });
        this.tableData = response.data.items;
        this.total = response.data.items.length;
        this.listFilter = response.data.items
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.allsearch {
    margin-top: 0;
    padding: 0;
  .search-content {
    width: 100%;
  }
}
</style>
